﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OTS.BLL;
using OTS.Model;

public partial class SysManage_Others_CreditLimitM : System.Web.UI.Page
{
    private DataTable dtlist = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
            Session["dtlist"] = null;
            //  权限
            SettingRight();
            // 初始化
            Clear();
            InitDataBind();
            BindTypeValue();
        }
    }
    // 页面权限
    private void SettingRight()
    {
        bool[] rightlist = SysRightBLL.GetRightByType(new string[] { 
            Session["UserCode"].ToString(), 
            Session["CoCode"].ToString(), 
            "mcCreditCheck"});
        if (!rightlist[0])
        { this.Page.Response.Write(String.Format("<script language='javascript'>window.parent.location.href='{0}';</script>", CommonModel.loginOut2)); }

        gv.Columns[gv.Columns.Count - 2].Visible = rightlist[3];
        gv.Columns[gv.Columns.Count - 1].Visible = rightlist[2];
        //btnSave.Visible = rightlist[3];
        btnExport.Visible = rightlist[4];
        btnAdd.Visible = rightlist[1];
        hidadd.Value = rightlist[1] ? "1" : "0";
    }

    // 初始化绑定
    public void BindTypeValue()
    {
        string str = ddlTypeValue.Text.Trim();
        if (str.Length > 0)
        {
            for (int i = 0; i < ddlTypeValue.Items.Count; i++)
            {
                if (ddlTypeValue.Items[i].Text.Contains(str))
                {
                    ddlTypeValue.SelectedIndex = i;
                    return;
                }
            }
        }
        int index = ddlType.SelectedIndex;
        DataTable dt = CreditLimitMBLL.GetTypeValueList(index, ddlTypeValue.Text);
        ddlTypeValue.DataSource = dt;
        if (index == 0)
        {
            ddlTypeValue.ValueField = "CustomerNo";
            ddlTypeValue.TextField = "CustomerName";
        }
        else
        {
            ddlTypeValue.ValueField = "OrderNo";
            ddlTypeValue.TextField = "OrderNo";
        }
        ddlTypeValue.DataBind();
        //ddlCompany.Items.Add("请选择 (Please Select)", "-1");
        //ddlcompanyimport.Items.Add("请选择 (Please Select)", "-1");
        ddlTypeValue.SelectedIndex = 0;
        ddlTypeValue.SelectedIndex = 0;
    }
    // 已有数据
    private void InitDataBind()
    {
        dtlist = new DataTable();
        if (Session["dtlist"] != null)
        {
            dtlist = Session["dtlist"] as DataTable;
        }
        else
        {
            dtlist = CreditLimitMBLL.GetList("");
        }
        gv.DataSource = dtlist;
        gv.DataBind();
        Session["dtlist"] = dtlist;
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        InitDataBind();
        gv.Columns[gv.Columns.Count - 1].Visible = false;
        gv.Columns[gv.Columns.Count - 2].Visible = false;
        grid.WriteXlsToResponse();
        gv.Columns[gv.Columns.Count - 1].Visible = true;
        gv.Columns[gv.Columns.Count - 2].Visible = true;
    }
    protected void gv_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
    {
        InitDataBind();
    }
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        Clear();
        BindTypeValue();
    }
    protected void ddlTypeValue_TextChanged(object sender, EventArgs e)
    {
        BindTypeValue();
    }
    protected void Clear()
    {
        ddlTypeValue.Text = "";
        txtAmount.Text = "0";
        txtValidDate.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
        txtInValidDate.Text = System.DateTime.Now.AddDays(7).ToString("yyyy-MM-dd");
        txtRemarks.Text = "";
    }
    protected void gv_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        try
        {
            string temp = e.CommandArgs.CommandArgument.ToString();
            int Inttemp = int.Parse(temp);
            string cmd = e.CommandArgs.CommandName;

            if (cmd == "delete")
            {
                bool result = CreditLimitMBLL.Delete(Inttemp);
                string message="操作失败";
                if (result)
                {
                    message = "操作成功";
                    Session["dtlist"] = null;
                    InitDataBind();
                }
                CommonMethod.regAlertMessage(Upd, message, this);
            }
            else if (cmd == "edit")
            {
                Clear();
                CreditLimitMModel model = CreditLimitMBLL.GetModel(Inttemp);
                string str = model.TypeValue;
                bool existsflag = false;
                if (str.Length > 0)
                {
                    if (model.Type == ddlType.SelectedIndex)
                    {
                        for (int i = 0; i < ddlTypeValue.Items.Count; i++)
                        {
                            if (ddlTypeValue.Items[i].Value.ToString().Contains(str))
                            {
                                ddlTypeValue.SelectedIndex = i;
                                existsflag = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        ddlTypeValue.Items.Clear();
                    }
                }
                if (!existsflag)
                {
                    ddlTypeValue.Items.Add(model.Type == 0 ? model.TypeValueShow : model.TypeValue, model.TypeValue);
                    ddlTypeValue.SelectedIndex = ddlTypeValue.Items.Count - 1;
                    ddlTypeValue.Text = model.Type == 0 ? model.TypeValueShow : model.TypeValue;
                }

                ddlType.SelectedIndex = model.Type;
                txtAmount.Text = model.Amount.ToString();
                txtValidDate.Text = CommonBLL.DateFormat(model.ValidDate.ToString(), "yyyy-MM-dd");
                txtInValidDate.Text = CommonBLL.DateFormat(model.InValidDate.ToString(), "yyyy-MM-dd");
                txtRemarks.Text = model.Remarks;
                hidid.Value = model.CreditLimitID.ToString();

                btnAdd.Visible = false;
                btnSave.Visible = true;
            }
        }
        catch (Exception ex)
        {
            CommonMethod.regAlertMessage(Upd, "出错啦！", this);
            Log.WritLog("gv_RowCommand:", ex.ToString());
        }
    }
    private CreditLimitMModel SetModel(CreditLimitMModel tempmodel)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);

        CreditLimitMModel smodel = tempmodel;

        smodel.Type = ddlType.SelectedIndex;
        smodel.TypeValue = ddlTypeValue.SelectedItem.Value.ToString().Trim();
        smodel.Amount = decimal.Parse(txtAmount.Text.Trim());
        smodel.ValidDate = DateTime.Parse(txtValidDate.Text.Trim());
        smodel.InValidDate = DateTime.Parse(txtInValidDate.Text.Trim());
        smodel.Remarks = txtRemarks.Text.Trim();
        smodel.ADDAUTHOR = Session["UserCode"] != null ? Session["UserCode"].ToString() : "10000";
        smodel.ADDDATE = DateTime.Parse(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        smodel.UPDATEAUTHOR = smodel.ADDAUTHOR;
        smodel.UPDATEDATE = smodel.ADDDATE;

        return smodel;
    }
    private bool Check()
    {
        try
        {
            if (string.IsNullOrEmpty(ddlTypeValue.Text.Trim()))
            {
                CommonMethod.regAlertMessage(Upd, "类型值不能为空！", this);
                return false;
            }
            else if (string.IsNullOrEmpty(txtAmount.Text.Trim()))
            {
                CommonMethod.regAlertMessage(Upd, "金额值不能为空！", this);
                return false;
            }
            else if (!CommonMethod.CheckNUM(txtAmount.Text.Trim()))
            {
                CommonMethod.regAlertMessage(Upd, "金额必须为数字！", this);
                return false;
            }
            else if (txtAmount.Text.Trim()=="0")
            {
                CommonMethod.regAlertMessage(Upd, "金额不能为：0 ", this);
                return false;
            }
            else if (string.IsNullOrEmpty(txtValidDate.Text.Trim()))
            {
                CommonMethod.regAlertMessage(Upd, "有效日期不能为空！", this);
                return false;
            }
            else if (string.IsNullOrEmpty(txtInValidDate.Text.Trim()))
            {
                CommonMethod.regAlertMessage(Upd, "有效日期不能为空！", this);
                return false;
            }
            DateTime.Parse(txtValidDate.Text.Trim());
            DateTime.Parse(txtInValidDate.Text.Trim());
        }
        catch (Exception ex)
        {
            CommonMethod.regAlertMessage(Upd, ex.ToString(), this);
            return false;
        }

        return true;
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        bool flag = Check();
        if (flag)
        {
            CreditLimitMModel model = SetModel(new CreditLimitMModel());
            flag = CreditLimitMBLL.Add(model);
            string message = "添加失败！";
            if (flag)
            {
                message = "添加成功！";
                Session["dtlist"] = null;
                InitDataBind();
            }
            CommonMethod.regAlertMessage(Upd, message, this); 
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool flag = Check();
        if (flag)
        {
            CreditLimitMModel model = SetModel(new CreditLimitMModel());
            model.CreditLimitID = long.Parse(hidid.Value);
            flag = CreditLimitMBLL.Update(model);
            string message = "更新失败！";
            if (flag)
            {
                Session["dtlist"] = null;
                InitDataBind();
                message = "更新成功！";
                Clear(); 
                btnAdd.Visible = hidadd.Value == "1";
                btnSave.Visible = false;
            }
            CommonMethod.regAlertMessage(Upd, message, this);
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Clear();
        btnAdd.Visible = hidadd.Value == "1";
        btnSave.Visible = false;
    }
}